﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "lpiece.h"
#include "board.h"
#include <iostream>


//teste do comparador do svn

MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent), m_Ui(new Ui::MainWindow), m_Board(new Board()), m_Timer(new QTimer(this))
{
    m_Ui->setupUi(this);
    m_Ui->boardView->setScene(m_Board);
    m_Ui->boardView->installEventFilter(this);

    m_Ui->nextPieceView->setScene(m_Board->getNextPieceScene());
    m_Ui->nextPieceView->installEventFilter(this);

    connect(m_Ui->startButton, SIGNAL(clicked()), this, SLOT(start()));
    connect(m_Timer, SIGNAL(timeout()), this, SLOT(updateCaption()));
}

void MainWindow::start()
{
    if (m_Timer->isActive())
    {
        m_Board->clearData();
        m_Board->randPiece();
        m_Ui->pontuacao->setText(QString::number(m_Board->getScore()));
        m_Ui->level->setText(QString::number(m_Board->getLevel().getLevelValue()));
        m_Timer->start(m_Board->getLevel().getTimer());
    }
    else
    {
        m_Timer->start(m_Board->getLevel().getTimer());
        m_Board->clearData();
        m_Board->randPiece();
        m_Ui->startButton->setText("Restart");
    }
}

void MainWindow::pause()
{
    m_Timer->stop();
}

MainWindow::~MainWindow()
{
    delete m_Ui;
    delete m_Board;
    delete m_Timer;
}

bool MainWindow::eventFilter(QObject*, QEvent *event)
{
    if (event->type() == QEvent::KeyPress && !m_Board->isGameOver())
    {
        QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(event);
        if(keyEvent->text() == "p") // < key
        {
            if (m_Timer->isActive())
            {
                m_Board->clear();
                m_Board->getNextPieceScene()->clear();
                pause();
            }
            else
            {
                if(m_Ui->startButton->text() != "Restart"){
                    start();
                }else{
                    updateCaption();
                    m_Timer->start(m_Board->getLevel().getTimer());
                }
            }
        }
        if(keyEvent->key() == Qt::Key_Left) // < key
        {
            if (m_Timer->isActive())
              m_Board->tryMoveLeft();
        }
        if(keyEvent->key() == Qt::Key_Right) // < key
        {
            if (m_Timer->isActive())
              m_Board->tryMoveRight();
        }
        if(keyEvent->key() == Qt::Key_Down) // < key
        {
            if (m_Timer->isActive())
              m_Board->tryMoveDown();
        }
        if(keyEvent->key() == Qt::Key_Up) // < key
        {
            if (m_Timer->isActive())
              m_Board->tryRotation();
        }
        return true;
    }
    return false;
}

void MainWindow::updateCaption()
{
    int scoreAtual = m_Board->getScore();
    {
        m_Ui->level->setText(QString::number(m_Board->getLevel().getLevelValue()));
        m_Timer->start(m_Board->getLevel().getTimer());
    }
    m_Ui->pontuacao->setText(QString::number(scoreAtual));
    if (m_Board->isGameOver())
    {
      m_Timer->stop();
      QMessageBox(QMessageBox::Information,"Game Over", "Acabou o jogo",QMessageBox::Ok).exec();
    }
    else
        m_Board->tryMoveDown();
}

void MainWindow::show(){
    setVisible(true);
    m_Board->redesenheBoard();
    m_Ui->boardView->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_Ui->nextPieceView->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_Board->clear();
    m_Board->getNextPieceScene()->clear();
}

